package logic

import (
	"context"
	"shop-ease/shopRpc/shopRpc"

	"shop-ease/shopApi/internal/svc"
	"shop-ease/shopApi/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type ProductAddLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewProductAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProductAddLogic {
	return &ProductAddLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ProductAddLogic) ProductAdd(req *types.ProductAddRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	if req.Stock == 0 || req.Img == "" || req.Price == 0.0 || req.Name == "" {
		return &types.Response{
			Message: "数据不完整，请补充数据后再添加",
			Code:    500,
			Data:    nil,
		}, nil
	}
	data, err := l.svcCtx.ShopRpc.ProductAdd(l.ctx, &shopRpc.ProductAddRequest{
		Name:   req.Name,
		Price:  float32(req.Price),
		Stock:  req.Stock,
		Img:    req.Img,
		Status: req.Status,
	})
	if err != nil {
		return &types.Response{
			Message: err.Error(),
			Code:    500,
			Data:    nil,
		}, nil
	}
	return &types.Response{
		Message: "商品添加成功",
		Code:    200,
		Data:    data,
	}, nil

}
